package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.domain.MaterialDown;
import com.ruoyi.system.service.IMaterialDownService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 下载记录Controller
 * 
 * @author ruoyi
 * @date 2023-12-04
 */
@RestController
@RequestMapping("/down")
public class MaterialDownController extends BaseController
{
    @Autowired
    private IMaterialDownService materialDownService;

    /**
     * 查询下载记录列表
     */
    @RequiresPermissions("system:down:list")
    @GetMapping("/list")
    public TableDataInfo list(MaterialDown materialDown)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            materialDown.setAppId(user.getAppId());
        }
        startPage();
        List<MaterialDown> list = materialDownService.selectMaterialDownList(materialDown);
        return getDataTable(list);
    }

    /**
     * 导出下载记录列表
     */
    @RequiresPermissions("system:down:export")
    @Log(title = "下载记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaterialDown materialDown)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            materialDown.setAppId(user.getAppId());
        }
        List<MaterialDown> list = materialDownService.selectMaterialDownList(materialDown);
        ExcelUtil<MaterialDown> util = new ExcelUtil<MaterialDown>(MaterialDown.class);
        util.exportExcel(response, list, "下载记录数据");
    }

    /**
     * 获取下载记录详细信息
     */
    @RequiresPermissions("system:down:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(materialDownService.selectMaterialDownById(id));
    }

    /**
     * 新增下载记录
     */
    @RequiresPermissions("system:down:add")
    @Log(title = "下载记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MaterialDown materialDown)
    {
        return toAjax(materialDownService.insertMaterialDown(materialDown));
    }

    /**
     * 修改下载记录
     */
    @RequiresPermissions("system:down:edit")
    @Log(title = "下载记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MaterialDown materialDown)
    {
        return toAjax(materialDownService.updateMaterialDown(materialDown));
    }

    /**
     * 删除下载记录
     */
    @RequiresPermissions("system:down:remove")
    @Log(title = "下载记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(materialDownService.deleteMaterialDownByIds(ids));
    }
}
